package com.gtxc.kotlinmvvm.ui.fragment


import android.content.Intent
import android.util.Log
import com.aleyn.mvvm.base.BaseFragment
import com.blankj.utilcode.util.ToastUtils
import com.gtxc.kotlinmvvm.BR
import com.gtxc.kotlinmvvm.LoginViewModel
import com.gtxc.kotlinmvvm.R
import com.gtxc.kotlinmvvm.databinding.FragmentLoginBinding
import com.gtxc.kotlinmvvm.network.request.LoinBean
import com.gtxc.kotlinmvvm.ui.activity.ForgetPasswordActivity
import com.gtxc.kotlinmvvm.ui.activity.HomeActivity
import com.gtxc.kotlinmvvm.utlis.setOnclickNoRepeat
import com.gtxc.kotlinmvvm.utlis.toStartActivity


class LoginFragment : BaseFragment<LoginViewModel, FragmentLoginBinding>(){


    override fun lazyLoadData() {
        viewModel.editTextUsernameValue.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed04?.setText(it)
        })
        viewModel.editTextPasswordValue.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)
            mBinding?.ed06?.setText(it)
        })
        viewModel.ok.observe(this, {
            Log.e("TAccccG", "onChanged: 数据有更新" + it)

            if (it){
                startActivity(Intent(activity, HomeActivity::class.java))
            }else{
                ToastUtils.showLong(viewModel.magess.value)
            }
        })
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(mBinding?.butLogin,mBinding?.tv08){
            when (it.id) {
                R.id.but_login -> {
                    login()
                }
                R.id.tv_08 ->{
                    //    忘记密码
                    startActivity(Intent(activity, ForgetPasswordActivity::class.java))
                } } } }
    //登录
    fun login(){
        when {
            mBinding?.ed04?.text.toString().isEmpty() -> ToastUtils.showLong("手机号不能为空")
            mBinding?.ed06?.text.toString().isEmpty() -> ToastUtils.showLong("密码不能为空")
            else ->
                viewModel.Login(
                    LoinBean(
                        mBinding?.ed06?.text.toString(),
                        mBinding?.ed04?.text.toString()
                    )
                )
        }
    }


    override fun initVariableID(): Int {
        return BR.viewModels
    }

    override fun layoutId(): Int {
        return R.layout.fragment_login
    }

}